var xmlHttp=GetXmlHttpObject();
var empty;
var mydialog;
var uname="";

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
	// Firefox, Opera 8.0+, Safari
	xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
	// Internet Explorer
 try
  {
	 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
	 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}


function deleterole(id){
	if (xmlHttp==null)
    {
		alert ("Browser does not support HTTP Request")
		return false;
    }else{
    	    art.dialog({
    		title:'刪除確認',
    		content:'您確定要刪除該角色嗎？',
    		lock:true,
    		ok:function(){
    			var url="deleterole";
    			url=url+"?id="+id;
    			xmlHttp.onreadystatechange=del_stateChanged;
    			xmlHttp.open("GET",url,true);
    			xmlHttp.send(null);
    		},
    		cancel:function(){}
    	});
    }
}

function del_stateChanged(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var msg=xmlHttp.responseText;
		var info=eval('('+msg+')');
		if(info.status===1){
			art.dialog({
				title:'提示',
				content:"刪除成功",
				lock:true,
				ok:function(){
					location.reload();
				},
				width:300
			});
		}else{
			art.dialog({
				title:'刪除失敗',
				content:info.info,
				lock:true,
				ok:function(){},
				width:300
			});
		}

	}
}
var flag=true;
function myselect(where,id){
	var msg=$("#"+id).attr("checked");
	if(msg){
		flag=false;
	}else{
		flag=true;
	}
	if(flag){
		$("."+where).attr("checked","");
		flag=false;
	}else{
		$("."+where).attr("checked","checked");
		flag=true;
	}
}



function clearmycheckbox(){
	$(".checkbox input").attr("checked","");
}
function selectallcheckbox(){
	$(".checkbox input").attr("checked","checked");
}

function checkcheckbox(direct)
{
	//1為增加角色
	//2為編輯角色
	var action='';
	var operator=0;
	if(direct===1){
		action="add_role_save";
		operator=18;
	}else if(direct==2){
		action="role_edit_save";
		operator=17;
	}
	var ischecked=0;
	var role_id="";
	var check=$('.checkbox input').get();
	role_id=$("#role_id").attr("value");
	var role_name=$("#role_name").attr("value");
	if(role_name==null||role_name==''){
		art.dialog({
			title:'提示',
			content:'角色名稱不能為空',
			lock:true,
			ok:function(){},
			width:300
		});
	}else{
		var data=[];
		data[0]='';
		data[1]='';
		for(var i=0;i<check.length;i++){
			var msg=check[i]['checked'];
			if(msg){
				var checkType=check[i]["name"];
				if(checkType==="type_id"){
					data[0]+=check[i]["value"]+";";
				}else if(checkType==='content_id'){
					data[1]+=check[i]['value']+";";
				}
				ischecked++;
			}
		}
		if(ischecked===0){
			art.dialog({
				title:'提示',
				content:'請至少選擇一項權限',
				lock:true,
				ok:function(){},
				width:300
			});
		}else{
			mydialog=art.dialog({
				title:'處理中',
				content:empty,
				lock:true,
				width:300
			});
			var url=action;
			$.ajax({
				url:url,
				type:'get',
				dataType:'json',
				data:{type_id:data[0],content_id:data[1],role_id:role_id,role_name:role_name,operator:operator},
				success:function(data){
					mydialog.close();
					art.dialog({
						title:'提示',
						content:data.info,
						lock:true,
						ok:function(){
							location.href="auth_role?operator=15";
						},
						width:300
					});
				},
				error:function(){alert('error');}
			});
		}
	}
	

}

function selectheader(id){
	$('#'+id).attr("checked","checked");
}

